<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<ny_lizhong@126.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Office as OfficeModel;

/**
 * 机构控制器
 *
 * @author 李忠<ny_lizhong@126.com>
 * @createAt 2018-09-06 14:59:05
 *
 */
class Office extends Admin {

    /**
     * 列表页面
     */
    public function index($status = null, $type = null, $title = null, $pid = 0) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = OfficeModel::lists($status, $type, $title, $pid);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('office_tree', list_to_config(OfficeModel::toFormatTree('admin'), 'id', 'title_show'));

        $this->assign('pid', $pid);
        $this->assign('type', $type);
        $this->assign('title', $title);
        $this->assign('status', $status);
        $this->assign('ppid', OfficeModel::getPid($pid));
        $this->setMeta('机构管理');
        return $this->fetch();
    }

    // 新增机构
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Office');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = OfficeModel::create($data)) {
                $this->log($data['id'], '新增机构成功');
                $this->success('新增机构成功', cookie('__forward__'));
            } else {
                $this->error('新增机构失败');
            }
        } else {

            $info = array('id' => null, 'name' => null, 'title' => null, 'pid' => input('get.pid'), 'type' => null, 'sort' => 0);
            $this->assign('info', $info);
            $this->assign('office_parent', list_to_config(OfficeModel::toFormatTree('admin'), 'id', 'title_show'));
            $this->assign('office_type', config('office_type'));
            $this->setMeta('新增机构');
            return $this->fetch();
        }
    }

    // 更新机构
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Office');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = OfficeModel::update($data)) {

                $this->log($data['id'], '更新机构成功');
                $this->success('更新机构成功', cookie('__forward__'));
            } else {
                $this->error('更新机构失败');
            }

        } else {
            $info = OfficeModel::get($id);

            $this->assign('info', $info);
            $this->assign('office_parent', list_to_config(OfficeModel::toFormatTree('admin'), 'id', 'title_show'));
            $this->assign('office_type', config('office_type'));

            $this->setMeta('更新机构');
            return $this->fetch('add');
        }
    }

    /**
     * 排序
     */
    public function sort($pid = 0) {
        if ($this->request->isPost()) {
            $ids = input('ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                OfficeModel::where('id', $value)->update(['sort' => $key + 1]);
            }

            $this->success('部门排序成功！');

        } else {
            // 获取排序的数据
            $map = array(['status', '=', 1], ['pid', '=', $pid]);

            $data_list = OfficeModel::where($map)->field('id,name,title')->order('sort asc')->select();
            $this->assign('data_list', $data_list);
            $this->setMeta('部门排序');
            return $this->fetch();
        }
    }

    public function delete($id) {
        if (OfficeModel::state($id, -1)) {
            $this->log($id, '删除机构成功');
            $this->success('删除机构成功');
        } else {
            $this->error('删除机构失败');
        }
    }

    public function config() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Office');
            if (true !== $result) {
                $this->error($result);
            }
            $data['id'] = CID;
            if ($data = OfficeModel::update($data)) {

                $this->log($data['id'], '更新公司信息成功');
                $this->success('更新公司信息成功');
            } else {
                $this->error('更新公司信息失败');
            }
        } else {
            $info = OfficeModel::get(CID);

            $this->assign('info', $info);
            $this->setMeta('公司信息');
            return $this->fetch();
        }
    }
}